package com.huice.day11;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import com.huice.Interface.Md5Tool;


public class HttpUtil {

	public static String STATUS = "status";
	public static String RESPONSE = "response";
	public static String TIME = "time";
	public static String HOST = "api.lixing.biz/service";
	public static String USER = "C100000362";
	public static String KEY = "4a071cd838d7fbeec1225dd7d125be3a";

	public static Map<String, String> doGet(String host, Map<String, String> headers, Map<String, String> parameters){
		int statusCode = 0;
		String responseString = null;
		String time = null;
		Map<String, String> result = new HashMap<String, String>();
		HttpClient client = HttpClients.createDefault();
		URIBuilder builder = new URIBuilder();
		builder.setScheme("http");
		builder.setHost(host);
		if(parameters!=null && parameters.size()>0){
			for(String key:parameters.keySet()){
				if(key!=null){
					builder.setParameter(key, parameters.get(key));
				}
			}
		}
		try {
			Date start = new Date();
			HttpGet get = new HttpGet(builder.build());
			if(headers!=null && headers.size()>0){
				for(String key:headers.keySet()){
					if(key!=null){
						get.setHeader(key, parameters.get(key));
					}
				}
			}
			HttpResponse response = client.execute(get);
			statusCode = response.getStatusLine().getStatusCode();
			if(statusCode==200){
				time = String.valueOf(new Date().getTime()-start.getTime());
				responseString = stream2string(response.getEntity().getContent());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("status", String.valueOf(statusCode));
		result.put("response", responseString);
	    result.put("time", time);	    
		return result;
	}
	
	public static String stream2string(InputStream in){
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			 br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			while((line = br.readLine())!=null){
				sb.append(line);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(br!=null){
					br.close();
				}		
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	public static Map<String, String> doPostByURL(String host, Map<String, String> headers, Map<String, String> parameters){
		int statusCode = 0;
		String responseString = null;
		String time = null;
		Map<String, String> result = new HashMap<String, String>();
		HttpClient client = HttpClients.createDefault();
		URIBuilder builder = new URIBuilder();
		builder.setScheme("http");
		builder.setHost(host);
		if(parameters!=null && parameters.size()>0){
			for(String key:parameters.keySet()){
				if(key!=null){
					builder.setParameter(key, parameters.get(key));
				}
			}
		}
		try {
			Date start = new Date();
			HttpPost post = new HttpPost(builder.build());
			if(headers!=null && headers.size()>0){
				for(String key:headers.keySet()){
					if(key!=null){
						post.setHeader(key, parameters.get(key));
					}
				}
			}
			HttpResponse response = client.execute(post);
			statusCode = response.getStatusLine().getStatusCode();
			if(statusCode==200){
				time = String.valueOf(new Date().getTime()-start.getTime());
				responseString = stream2string(response.getEntity().getContent());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("status", String.valueOf(statusCode));
		result.put("response", responseString);
	    result.put("time", time);	    
		return result;
	}

	public static Map<String, String> doPost(String host, Map<String, String> headers, Map<String, String> parameters){
		int statusCode = 0;
		String responseString = null;
		String time = null;
		Map<String, String> result = new HashMap<String, String>();
		HttpClient client = HttpClients.createDefault();
		URIBuilder builder = new URIBuilder();
		builder.setScheme("http");
		builder.setHost(host);
		try {
			Date start = new Date();
			HttpPost post = new HttpPost(builder.build());
			if(parameters!=null && parameters.size()>0){
				List<NameValuePair> list = new ArrayList<NameValuePair>(parameters.size());
				for(String key:parameters.keySet()){
					list.add(new BasicNameValuePair(key, parameters.get(key)));
				}
				post.setEntity(new UrlEncodedFormEntity(list));
			}
			if(headers!=null && headers.size()>0){
				for(String key:headers.keySet()){
					if(key!=null){
						post.setHeader(key, parameters.get(key));
					}
				}
			}
			HttpResponse response = client.execute(post);
			statusCode = response.getStatusLine().getStatusCode();
			if(statusCode==200){
				time = String.valueOf(new Date().getTime()-start.getTime());
				responseString = stream2string(response.getEntity().getContent());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("status", String.valueOf(statusCode));
		result.put("response", responseString);
	    result.put("time", time);	    
		return result;
	}
	
	public static String createSign(String para, String key){
		String source = null;
		if(para!=null){
			source = "para" + para + key; 
		}else{
			source = key;
		}
		return Md5Tool.getInstance().getLongToken(source, "UTF-8");
	}
}
